//
//  WXVistorView.swift
//  weibo
//
//  Created by 王旭 on 16/4/16.
//  Copyright © 2016年 王旭. All rights reserved.
//

import UIKit
import SnapKit

//指定按钮点击协议
@objc protocol WXVistorViewDelegate : NSObjectProtocol {
    func registerButtonClick()
    func loginButtonClick()
}

class WXVistorView: UIView {
    
    //遵守协议
   weak var delegate :WXVistorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //监听代理方法
    func registerButtonClick() {
        delegate?.registerButtonClick()
    }
    
    func loginButtonClick() {
        delegate?.loginButtonClick()
    }
    
    //提供方法设置显示视图
    func setVistorView(text:String?, image:String?){
        
        if let t = text, im = image {
            textLabel.text = t
            iconImgView.image = UIImage(named: im)
        }else{
            //是首页,执行动画
            startAni()
        }

    }
    
    //动画方法
    func startAni() {
        //显示旋转视图
        rotateImgView.hidden = false
        //核心动画
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        
        ani.duration = 10
        ani.repeatCount = MAXFLOAT
        ani.toValue = 2 * M_PI
        
        //解决界面切换，或者app退出到后台，默认会把动画给释放掉的问题
        ani.removedOnCompletion = false
        
        //添加到layer上
        rotateImgView.layer.addAnimation(ani, forKey: nil)
    }

    //界面布局
    func setupUI() {
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        //加载控件
        addSubview(rotateImgView)
        addSubview(maskImgView)
        addSubview(iconImgView)
        addSubview(textLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //第三方设置约束简化
         //房屋图片
        iconImgView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
         //旋转图片
        rotateImgView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconImgView)
        }
         //遮盖图片
        maskImgView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconImgView)
        }
         //文本label
        textLabel.snp_makeConstraints(closure: { (make) -> Void in
            make.centerX.equalTo(iconImgView)
            make.top.equalTo(maskImgView.snp_bottom).offset(15)
        })
         //注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(textLabel)
            make.top.equalTo(textLabel.snp_bottom).offset(30)
            make.width.equalTo(80)
        }
         //登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(textLabel)
            make.top.equalTo(registerButton)
            make.width.equalTo(registerButton)
        }
        
    }
//第三方设置约束未简写顶-----------------------------------------------
//    //房屋图片
//    iconImgView.snp_makeConstraints { (make) -> Void in
//    make.centerX.equalTo(self.snp_centerX)
//    make.centerY.equalTo(self.snp_centerY)
//    }
//    //旋转图片
//    rotateImgView.snp_makeConstraints { (make) -> Void in
//    make.centerX.equalTo(self.iconImgView.snp_centerX)
//    make.centerY.equalTo(self.iconImgView.snp_centerY)
//    }
//    //遮盖图片
//    maskImgView.snp_makeConstraints { (make) -> Void in
//    make.centerX.equalTo(self.iconImgView.snp_centerX)
//    make.centerY.equalTo(self.iconImgView.snp_centerY)
//    }
//    //文本label
//    textLabel.snp_makeConstraints { (make) -> Void in
//    make.centerX.equalTo(self.iconImgView.snp_centerX)
//    make.top.equalTo(self.maskImgView.snp_bottom).offset(15)
//    }
//    //注册按钮
//    registerButton.snp_makeConstraints(closure: { (make) -> Void in
//    make.leading.equalTo(self.textLabel.snp_leading)
//    make.top.equalTo(self.textLabel.snp_bottom).offset(30)
//    make.width.equalTo(80)
//    }
//    //登录按钮
//    loginButton.snp_makeConstraints(closure: { (make) -> Void in
//    make.trailing.equalTo(self.textLabel.snp_trailing)
//    make.top.equalTo(self.registerButton.snp_top)
//    make.width.equalTo(self.registerButton.snp_width)
//    }
//第三方设置约束未简写底-----------------------------------------------
    
    
//使用系统的约束设置顶--------------------------------------------------
//    //房屋图片
//    iconImgView.translatesAutoresizingMaskIntoConstraints = false
//    addConstraint(NSLayoutConstraint(item: iconImgView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//    addConstraint(NSLayoutConstraint(item: iconImgView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
//    //旋转图片
//    rotateImgView.translatesAutoresizingMaskIntoConstraints = false
//    addConstraint(NSLayoutConstraint(item: rotateImgView, attribute: .CenterX, relatedBy: .Equal, toItem: iconImgView, attribute: .CenterX, multiplier: 1, constant: 0))
//    addConstraint(NSLayoutConstraint(item: rotateImgView, attribute: .CenterY, relatedBy: .Equal, toItem: iconImgView, attribute: .CenterY, multiplier: 1, constant: 0))
//    //遮盖图片
//    maskImgView.translatesAutoresizingMaskIntoConstraints = false
//    addConstraint(NSLayoutConstraint(item: maskImgView, attribute: .CenterX, relatedBy: .Equal, toItem: iconImgView, attribute: .CenterX, multiplier: 1, constant: 0))
//    addConstraint(NSLayoutConstraint(item: maskImgView, attribute: .CenterY, relatedBy: .Equal, toItem: iconImgView, attribute: .CenterY, multiplier: 1, constant: 0))
//    //文本label
//    textLabel.translatesAutoresizingMaskIntoConstraints = false
//    addConstraint(NSLayoutConstraint(item: textLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconImgView, attribute: .CenterX, multiplier: 1, constant: 0))
//    addConstraint(NSLayoutConstraint(item: textLabel, attribute: .Top, relatedBy: .Equal, toItem: maskImgView, attribute: .Bottom, multiplier: 1, constant: 15))
//    //注册按钮
//    registerButton.translatesAutoresizingMaskIntoConstraints = false
//    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
//    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 30))
//    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 80))
//    //登录按钮
//    loginButton.translatesAutoresizingMaskIntoConstraints = false
//    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
//    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 80))
//使用系统的约束设置底--------------------------------------------------
    
    
    //---懒加载控件---
    
    //房屋图片
//    private lazy var iconImgView : UIImageView = {()->UIImageView in
//        let iconImgView = UIImageView()
//        iconImgView.image = UIImage(named: "visitordiscover_feed_image_house")
//        return iconImgView
//    }()
    private lazy var iconImgView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //旋转图片
    private lazy var rotateImgView : UIImageView = {
      let rotateImgView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        rotateImgView.hidden = true
        return rotateImgView
    }()
    
    //遮盖图片
    private lazy var maskImgView : UIImageView = {
      let maskImgView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return maskImgView
    }()
    
    //文本label
    private lazy var textLabel : UILabel = {()->UILabel in
        
        //正常实例化
//        let label = UILabel()
        //通过分类的便利方法实例化
//        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14, preferredMaxLayoutWidth: 200)

        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //对齐方式
        label.textAlignment = .Center
        
//通过分类实现了以下代码------------------------------------------
//        //颜色
//        label.textColor = UIColor.darkGrayColor()
//        //字体大小
//        label.font = UIFont.systemFontOfSize(14)
//        //多行设置
//        label.numberOfLines = 0
//        label.preferredMaxLayoutWidth = 200
//通过分类实现了以下代码------------------------------------------
        
        return label
    }()
    
    //注册按钮
    private lazy var registerButton : UIButton = {()->UIButton in
        
        //正常实例化
//        let button = UIButton()
        //通过分类实例化
        let button = UIButton(titleColor: UIColor.orangeColor(), fontSize: 14)
        
        //添加事件
        button.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置文本
        button.setTitle("注册", forState: UIControlState.Normal)
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
//通过分类实现了以下代码------------------------------------------
//        //设置文本颜色
//        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
//        //设置文本字体大小
//        button.titleLabel?.font = UIFont.systemFontOfSize(14)
//通过分类实现了以下代码------------------------------------------

        return button
    }()
    
    //登录按钮
    private lazy var loginButton : UIButton = {()->UIButton in
        
        //通过分类实例化
        let button = UIButton(titleColor: UIColor.orangeColor(), fontSize: 14)
        
        //添加事件
        button.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置文本
        button.setTitle("登录", forState: UIControlState.Normal)
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
        return button
    }()

}
